package com.qy139.auth.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qy139.util.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Component
public class MyAceessDefine implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {


        // 成功的信息   返回出去
        ObjectMapper objectMapper = new ObjectMapper();
        Result result = new Result();
        result.setCode(403);
        result.setMsg("权限不允许");
        result.setData(null);
        // result
        String s = objectMapper.writeValueAsString(result);
        httpServletResponse.setContentType("application/json;charset=utf8");
        // 响应
        PrintWriter out = httpServletResponse.getWriter();
        out.print(s);
        out.flush();
        out.close();
    }
}
